package com.trex.modules.utils;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;

public class JsonTest {

    public static void main(String[] args) throws JsonProcessingException {
        // 调用生成 JSON 的方法
        String json = generateJson();
        System.out.println(json);
    }

    public static String generateJson() throws JsonProcessingException {
        // 设置时间戳
        String timestampWithLast4Millis = new SimpleDateFormat("yyyyMMddHHmmssSSS").format(System.currentTimeMillis());
        String idN = "12345"; // 示例 ID
        String userName = "testUser"; // 示例用户名
        String formattedDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(System.currentTimeMillis());

        // SysInfo 数据
        Map<String, Object> sysInfo = Map.of(
                "Flag", 0,
                "Msg", "",
                "SvcName", "NEM216",
                "UUID", timestampWithLast4Millis
        );


        // BODY 表的行数据（作为 Map，而不是 List）
        List<Map<String, Object>> bodyRows = List.of(
                Map.of(
                        "OPERATION_FLAG", "I",
                        "STATUS", 1,
                        "REC_ID", idN,
                        "WORK_TIME", formattedDate,
                        "REC_REVISOR", userName
                )
        );
// 构建 Tables 数据
        Map<String, Object> tables = Map.of(
                "BODY", bodyRows
        );

// 构建最终的 JSON 数据结构
        Map<String, Object> finalData = Map.of(
                "SysInfo", sysInfo,
                "Tables", tables
        );
        ObjectMapper objectMapper = new ObjectMapper();
        String jsonMap = objectMapper.writeValueAsString(finalData);

        return jsonMap;
    }

}

